package com.junjie.checkin.controller;

import com.junjie.checkin.exception.ResourceNotFountException;
import com.junjie.checkin.request.activity.ActivityParticipationRequest;
import com.junjie.checkin.request.user.UserPageRequest;
import com.junjie.checkin.request.user.UserRequest;
import com.junjie.checkin.request.user.UserScoreUpdateRequest;
import com.junjie.checkin.request.user.UserUpdateRequest;
import com.junjie.checkin.response.PageResult;
import com.junjie.checkin.response.Result;
import com.junjie.checkin.response.ResultStatus;
import com.junjie.checkin.response.admin.*;
import com.junjie.checkin.service.AdminActivityService;
import com.junjie.checkin.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user")
public class AdminUserController {
    @Autowired
    private UserService userService;
    @Autowired
    private AdminActivityService adminActivityService;

    /**
     * 根据id查询用户 (已完成)
     * @param id 要查询的用户ID
     * @return 查询到的用户信息
     */
    @GetMapping("/{id}")
    public Result<UserResponse> getUserById(@PathVariable Integer id){
        UserResponse user = userService.getUserById(id);
        return Result.getResult(ResultStatus.OK,user);
    }

    /**
     * 获取用户列表 (已完成)
     * @param userPageRequest 查询参数
     * @return 统一返回结果,返回数据为用户列表
     */
    @GetMapping("")
    public Result<PageResult<UserResponse>> getUsers(UserPageRequest userPageRequest){
        // 初始化,防止page和pageSize为null
        userPageRequest.initDefaults();
        PageResult<UserResponse> pageResult = userService.getUsers(userPageRequest);
        return Result.getResult(ResultStatus.OK,pageResult);
    }

    /**
     * 获取学生列表 (已完成)
     * @param userPageRequest 查询参数
     * @return 统一返回结果,返回数据为学生列表
     */
    @GetMapping("/students")
    public Result<PageResult<UserResponse>> getStudents(UserPageRequest userPageRequest){
        // 初始化,防止page和pageSize为null
        userPageRequest.initDefaults();
        PageResult<UserResponse> pageResult = userService.getStudents(userPageRequest);
        return Result.getResult(ResultStatus.OK,pageResult);
    }

    /**
     * 添加用户
     * @param userRequest 添加的用户信息
     * @return 统一返回结果,返回数据为添加的用户信息
     */
    @PostMapping("")
    public Result<UserResponse> addUser(@RequestBody UserRequest userRequest , HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserResponse userResponse = userService.insertAdmin(userRequest, token);
        return Result.getResult(ResultStatus.OK,userResponse);
    }

    /**
     * 添加学生信息
     * @param userRequest 添加的用户信息
     * @param request 请求
     * @return 统一返回结果,返回数据为添加的用户信息
     */
    @PostMapping("/student")
    public  Result<UserResponse> addStudent(@RequestBody UserRequest userRequest, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserResponse userResponse = userService.insertStudent(userRequest, token);
        return Result.getResult(ResultStatus.OK,userResponse);
    }

    /**
     * 修改用户 TODO  角色不允许修改,这边需要调整
     * @param userRequest 修改的用户信息
     * @param request  http请求,获取当前登录用户信息
     * @return 统一返回结果,无返回值
     */
    @PutMapping("/{id}")
    public Result updateUser(@PathVariable Integer id,@RequestBody UserUpdateRequest userRequest, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        userService.updateUser( id,userRequest,token);
        return Result.getResult(ResultStatus.OK);
    }

    /**
     * 删除用户
     * @param id 删除的用户ID
     * @return 统一返回结果,无返回值
     */
    @DeleteMapping("/{id}")
    public Result deleteUser(@PathVariable Integer id,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        userService.deleteUser(id,token);
        return Result.getResult(ResultStatus.OK);
    }

    /**
     * 修改用户积分
     * @param id 修改的用户ID
     * @param updateRequest  更新积分的数据
     * @param request http请求,获取当前登录用户信息
     * @return 统一返回结果,返回数据为修改后的用户信息和积分记录
     */
    @PostMapping("/{id}/score")
    public Result<UserScoreUpdateResponse> updateScore(@PathVariable Integer id, @RequestBody UserScoreUpdateRequest updateRequest, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserScoreUpdateResponse updateResponse = userService.updateUserScore(id, updateRequest, token);
        return Result.getResult(ResultStatus.OK,updateResponse);
    }

    /**
     * 获取用户积分
     * @param id 用户ID
     * @return 统一返回结果,返回数据为用户积分
     */
    @GetMapping("/points/{id}")
    public Result getUserPoints(@PathVariable Integer id){
        if(id == null){
            throw new ResourceNotFountException();
        }
        UserPointsResponse pointsResponse =userService.getUserPoints(id);
        return Result.getResult(ResultStatus.OK,pointsResponse);
    }

    /**
     * 获取用户积分详情
     * @param id
     * @return
     */
    @GetMapping("/points/details/{id}")
    public Result getUserPointsDetails(@PathVariable Integer id){
        if(id == null){
            throw new ResourceNotFountException();
        }
        UserPointsDetailsResponse userPointsDetailsResponse = userService.getUserPointsDetails(id);
        return Result.getResult(ResultStatus.OK,userPointsDetailsResponse);
    }

    /**
     * 活动参与记录查询
     * @param
     * @return
     */
    @GetMapping("/activities/join")
    public Result<PageResult<ActivityParticipationResponse>> getJoinPageActivities(ActivityParticipationRequest request){
        request.initDefaults();
        PageResult<ActivityParticipationResponse> pageResult = adminActivityService.getJoinPageActivities(request);
        return Result.getResult(ResultStatus.OK,pageResult);
    }
}
